<?php

namespace App\Task\Stat;

use App\Model\AppChannel;
use App\Model\Stat\StatOpeninstall;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 每日统计-openinstall数据
 * @Crontab(name="StatDailyOpeninstallTask", rule="*\/15 * * * *", callback="execute", memo="每日openinstall数据", singleton=false)
 */
class StatDailyOpeninstallTask
{
    public function execute()
    {
        log_info(__CLASS__ . "==Start");
        for ($day = 1; $day >= 0; $day--) {
            $this->runDaily($day);
        }
    }

    function runDaily($day = 0)
    {
        $channels = AppChannel::getAllChannels(true);
        if (time() - strtotime(date('Y-m-d')) < 300) {
            $day++;
        }
        $start = date('Y-m-d', strtotime("-{$day} day"));
        $nextDay = $day - 1;
        $end = date('Y-m-d', strtotime("-{$nextDay} day"));
        // $channels[] = 'all';
        foreach ($channels as $channel => $packName) {
            // log_info(__CLASS__ . "=={$start}=={$end}=={$channel}=={$packName}");
            //日报表
            StatOpeninstall::updateRecord($start, $end, (string)$channel, $packName);
        }
    }
}
